#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Publish market data event to event queue
import event

class BaseMDEngine:
    """ Abstract class
    """
    __metaclass__ = ABCMeta

    @abstractmethod
    def publish(self):
        raise NotImplementedError("Any MD Engine should implement publish method.")


class SimpleFileMDEngine(BaseMDEngine):
    def __init__(self, event_queue, start_date, end_date, symbol):
        """ Load all market data into memory
        """
        self.event_queue = event_queue
        self.start_date = str(start_date)
        self.end_date = str(end_date)
        self.symbol = symbol
        self.md_event_list = self.load_mkt_data()

    def load_mkt_data(self):
        """ 
        Returns a sorted list of market data event
        """
        # TODO: load market from file
        # df

        # TODO: Transform dataframe/.. into list of market data event
        md_event_list = list()
        for x in df.iterrows():
            #
            md_event = event.MarketData(..)
            md_event_list.append(md_event)
        self._md_idx = 0
        self.publish_status = True
        return md_event_list

    def publish(self):
        if self._md_idx < len(self.md_event_list):
            # get first event from self.md_event_list
            md_event = self.md_event_list[self._md_idx]
            self.event_queue.put(md_event)
            self._md_idx = self._md_idx + 1
        else:
            self.publish_status = False
            # return False
            # raise an Exception

class KDBMDEngine(BaseMDEngine):
    def __init__(self):
        pass
    
    def publish(self):
        # use generator 
        pass